﻿/*  
 * Helix Engine
 * http://helixengine.codeplex.com/
 * 
 * Copyright (c) 2010 - 2011 Joe Hegarty
 *  
 * This source code is subject to the terms of the Microsoft Public License (Ms-PL). 
 *  
 * Redistribution and use in source and binary forms, with or without modification, 
 * is permitted provided that redistributions of the source code retain the above 
 * copyright notices and this file header. 
 * 
 * For details please see http://www.opensource.org/licenses/ms-pl.html 
 *  
 * All other rights reserved. 
 *
 */  


using System;
using System.Windows.Media.Imaging;

using HelixEngine;
using HelixEngine.Maths;
using HelixEngine.Resources;
using HelixEngine.Input;

namespace HelixGame.World.Entities.Components
{
    /// <summary>
    /// 
    /// </summary>
    public class HighlightComponent : EntityComponent
    {
        ResourcePackage _tilePackage;
        State _state = State.Idle;
        Entity _floorEntity;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="floorEntity"></param>
        public HighlightComponent(Entity floorEntity)
        {
            _floorEntity = floorEntity;
        }

        enum State
        {
            WaitingForPackage,
            DrawHighlight,
            Idle
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OnInitialised()
        {
            Parent.AddEC<GraphicAppearenceComponent>();
            GraphicAppearenceComponent graphicAppearance = null;

            if (Parent.GetEC(ref graphicAppearance))
            {
                Parent.AddComponentNeedingUpdate(this);

                _tilePackage = Engine.Get().ResourceManager.FetchPackage("room");
                _state = State.WaitingForPackage;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OnCleanup()
        {
            Parent.RemoveComponentNeedingUpdate(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        public override void Update(EngineTime time)
        {
            switch (_state)
            {
                case State.WaitingForPackage:
                    UpdateWaitingForPackage();
                    break;

                case State.DrawHighlight:
                    UpdateDrawHighlight();
                    break;


            }
        }

        void UpdateWaitingForPackage()
        {
            if (_tilePackage.IsComplete)
            {
                _state = State.DrawHighlight;
            }
        }

        void UpdateDrawHighlight()
        {
            GraphicAppearenceComponent graphicAppearance = null;

            if (Parent.GetEC(ref graphicAppearance))
            {
                string tilePath = "tiles/highlight.png";
                WriteableBitmap tile = _tilePackage.GetBitmap(tilePath);

                Vector2 mousePoint = Engine.Get().InputManager.GetMousePoint();
                IsometricPoint highlightTile = IsometricHelper.ScreenSpaceToWorldSpace(mousePoint);

                FloorComponent floor = null;
                if (_floorEntity.GetEC(ref floor))
                {
                    if (floor.IsTileValid(highlightTile))
                    {
                        Vector2 highlightPoint = IsometricHelper.WorldSpaceToScreenSpace(highlightTile);
                        int zindex = IsometricHelper.CalculateTileDepth(highlightTile) + IsometricSettings.FloorHighlightZOffset;
                        graphicAppearance.QueueWrite(zindex, tile, highlightPoint);
                    }
                }

                
            }
            else
            {
                _state = State.Idle;
            }
        }

    }
}
